package com.qfedu.buy.api.config;

import com.qfedu.buy.common.config.SystemConfig;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @program: NeighborBuy
 * @description: 解决Openfein无法传递请求对象的问题
 * @author: Feri(邢朋辉)
 * @create: 2021-01-28 11:22
 */
@Component
public class OpenFeignConfig implements RequestInterceptor{

    //解决自定义令牌的消息头和值的传递
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取请求对象
        ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        //获取要传递的请求消息头信息
//        Enumeration<String> enumeration= request.getHeaderNames();
//        while (enumeration.hasMoreElements()){
//            String h=enumeration.nextElement();
//            //把要传递的请求消息头信息 重新设置
//            requestTemplate.header(h,request.getHeader(h));
//        }
        requestTemplate.header(SystemConfig.TOKEN_HEADER,request.getHeader(SystemConfig.TOKEN_HEADER));
    }
}
